import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { AiPromptTemplatesApi } from '#/api/ai/prompt/promptTemplates';
import type { DescriptionItemSchema } from '#/components/description';

import { h } from 'vue';

import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';
import { IconifyIcon } from '@vben/icons';
import { handleTree } from '@vben/utils';

import { Typography } from 'ant-design-vue';

import { getLlmSimpleList } from '#/api/ai/llm/llmModel';
import { getPromptCategoriesList } from '#/api/ai/prompt/promptCategories';
import { getPromptTagPage } from '#/api/ai/prompt/promptTag';
import { ColorPicker } from '#/components/color-picker';
import { DictBadge } from '#/components/dict-badge';
import { Image } from '#/components/image';
import { getRangePickerDefaultProps } from '#/utils';
import { getFileUrl } from '#/utils/fileUtils';

const { Paragraph } = Typography;
export function useBaseFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'code',
      label: '提示词编号',
      component: 'Input',
      componentProps: {
        placeholder: '请输入提示词编号',
      },
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'name',
      label: '标题',
      rules: 'required',
      component: 'Input',
      componentProps: {
        placeholder: '请输入一个简介明了的标题（最多50个字符）',
        maxLength: 50,
      },
      help: '一个好的标题能够更好地吸引用户的注意力',
    },
    {
      fieldName: 'categoryId',
      label: '分类',
      component: 'ApiTreeSelect',
      componentProps: {
        allowClear: true,
        api: async () => {
          const data = await getPromptCategoriesList({});
          return handleTree(data);
        },
        labelField: 'name',
        valueField: 'id',
        childrenField: 'children',
        placeholder: '请选择提示词分类',
        treeDefaultExpandAll: true,
      },
      rules: 'selectRequired',
    },
    {
      fieldName: 'language',
      label: '语言',
      component: 'Select',
      defaultValue: 'zh_CN',
      componentProps: {
        options: getDictOptions(DICT_TYPE.AI_LANGUAGE),
        placeholder: '请选择语言',
      },
    },
    {
      fieldName: 'icon',
      label: '图标',
      component: 'IconPicker',
      componentProps: {
        placeholder: '请选择分类图标',
        prefix: 'carbon',
      },
    },
    {
      fieldName: 'color',
      label: '颜色',
      component: 'ColorPicker',
      componentProps: {
        placeholder: '请输入颜色',
        showAlpha: false,
      },
    },
    {
      fieldName: 'description',
      label: '描述',
      component: 'Textarea',
      componentProps: {
        placeholder: '简要的描述提示的功能和使用场景',
        rows: 4,
      },
      help: '清晰的描述有助于用户快速了解提示词的用途',
    },
    {
      fieldName: 'systemFlag',
      label: '系统内置',
      component: 'RadioGroup',
      componentProps: {
        options: [
          {
            label: '是',
            value: true,
          },
          {
            label: '否',
            value: false,
          },
        ],
        buttonStyle: 'solid',
        optionType: 'button',
      },
      defaultValue: false,
    },
    {
      fieldName: 'sort',
      label: '排序',
      component: 'InputNumber',
      componentProps: {
        min: 0,
        placeholder: '请输入排序',
      },
    },
    {
      fieldName: 'tagList',
      label: '标签',
      component: 'ApiPagingSelect',
      componentProps: {
        allowClear: true,
        api: getPromptTagPage,
        labelField: 'name',
        valueField: 'name',
        placeholder: '请选择标签',
        mode: 'tags',
      },
    },
  ];
}
export function useContentFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'content',
      label: '提示词内容',
      component: 'MarkdownEditor',
    },
  ];
}
export function useUsedFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'usageDescription',
      label: '提示词使用说明',
      component: 'Textarea',
      componentProps: {
        placeholder:
          '解释如何使用该提示词，可以包含示例，帮助他人理解如何使用你的提示词',
        rows: 6,
      },
      help: '详细的使用说明可以帮助其他用户更好地使用您的提示词',
    },
    {
      fieldName: 'modelType',
      label: '适用的AI模型',
      component: 'ApiSelect',
      componentProps: {
        api: getLlmSimpleList,
        labelField: 'title',
        valueField: 'id',
        mode: 'multiple',
        placeholder: '请选择适用的AI模型',
      },
    },
  ];
}
export function useEffectFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'effectImg',
      label: '效果展示图片',
      component: 'ImageUpload',
      componentProps: {
        params: {
          fileCategoriesCode: 'AISL', // 文件分类编码
          objectId: 'effectImg',
          objectType: '效果展示图片',
        },
        maxNumber: 10,
      },
    },
  ];
}
export function usePublishFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'versionNum',
      label: '版本号',
      component: 'Input',
      componentProps: {
        placeholder: '请输入版本号',
      },
      help: '修改提示词内容或变量，请更新版本号，系统根据版本号记录变更日志，可用于版本退回',
    },
    {
      fieldName: 'visibility',
      label: '可见性',
      component: 'Select',
      rules: 'selectRequired',
      defaultValue: 0,
      componentProps: {
        options: getDictOptions(DICT_TYPE.AI_PROMPT_VISIBILITY, 'number'),
        placeholder: '请选择可见性',
      },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'Select',
      rules: 'selectRequired',
      defaultValue: 0,
      componentProps: {
        options: getDictOptions(DICT_TYPE.AI_PROMPT_STATUS, 'number'),
        placeholder: '请选择状态',
      },
    },
    {
      fieldName: 'changeLog',
      label: '变更说明',
      component: 'Textarea',
      componentProps: {
        placeholder: '简要的描述变更说明',
        rows: 4,
      },
      help: '清晰的描述有助于用户快速了解提示词的变更内容',
      dependencies: {
        triggerFields: ['id'],
        show: (values) => {
          return values.id;
        },
      },
    },
  ];
}
/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'code',
      label: '提示词编号',
      component: 'Input',
      componentProps: {
        placeholder: '请输入提示词编号',
      },
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'name',
      label: '标题',
      rules: 'required',
      component: 'Input',
      componentProps: {
        placeholder: '请输入提示词名称',
      },
    },
    {
      fieldName: 'categoryId',
      label: '分类',
      component: 'ApiTreeSelect',
      componentProps: {
        allowClear: true,
        api: async () => {
          const data = await getPromptCategoriesList({});
          return handleTree(data);
        },
        labelField: 'name',
        valueField: 'id',
        childrenField: 'children',
        placeholder: '请选择上级提示词分类',
        treeDefaultExpandAll: true,
      },
      rules: 'selectRequired',
    },
    {
      fieldName: 'language',
      label: '语言',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.AI_LANGUAGE),
        placeholder: '请选择语言',
      },
    },
    {
      fieldName: 'tagList',
      label: '标签',
      component: 'ApiPagingSelect',
      componentProps: {
        allowClear: true,
        api: getPromptTagPage,
        labelField: 'name',
        valueField: 'name',
        placeholder: '请选择标签',
        mode: 'tags',
      },
    },
    {
      fieldName: 'icon',
      label: '提示词图标',
      component: 'IconPicker',
      componentProps: {
        placeholder: '请选择分类图标',
        prefix: 'carbon',
      },
    },
    {
      fieldName: 'color',
      label: '颜色',
      component: 'ColorPicker',
      componentProps: {
        placeholder: '请输入颜色',
        showAlpha: false,
      },
    },
    {
      fieldName: 'sort',
      label: '排序',
      component: 'InputNumber',
      componentProps: {
        min: 0,
        placeholder: '请输入排序',
      },
    },
    {
      fieldName: 'description',
      label: '提示词描述',
      component: 'Textarea',
      componentProps: {
        placeholder: '请输入提示词描述',
        rows: 4,
      },
    },
    {
      fieldName: 'usageDescription',
      label: '提示词使用说明',
      component: 'Textarea',
      componentProps: {
        placeholder: '请输入提示词使用说明',
        rows: 4,
      },
    },
    {
      fieldName: 'content',
      label: '提示词内容',
      rules: 'required',
      component: 'MarkdownEditor',
    },

    {
      fieldName: 'versionNum',
      label: '版本号',
      component: 'Input',
      componentProps: {
        placeholder: '请输入版本号',
      },
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },

    {
      fieldName: 'modelType',
      label: '适用的AI模型',
      component: 'ApiSelect',
      componentProps: {
        api: getSimpleList,
        labelField: 'title',
        valueField: 'id',
        mode: 'multiple',
        placeholder: '请选择适用的AI模型',
      },
    },
    {
      fieldName: 'visibility',
      label: '可见性',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.AI_PROMPT_VISIBILITY, 'number'),
        placeholder: '请选择可见性',
      },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.AI_PROMPT_STATUS, 'number'),
        placeholder: '请选择状态',
      },
    },
    {
      fieldName: 'systemFlag',
      label: '系统内置',
      component: 'RadioGroup',
      componentProps: {
        options: [
          {
            label: '是',
            value: true,
          },
          {
            label: '否',
            value: false,
          },
        ],
        buttonStyle: 'solid',
        optionType: 'button',
      },
    },

    {
      fieldName: 'metadata',
      label: '其他参数',
      component: 'Textarea',
      componentProps: {
        placeholder: '请输入其他参数使用json格式',
      },
    },
    {
      fieldName: 'effectImg',
      label: '效果展示图片',
      component: 'ImageUpload',
      componentProps: {
        params: {
          fileCategoriesCode: 'AISL', // 文件分类编码
          objectId: 'effectImg',
          objectType: '效果展示图片',
        },
        maxNumber: 10,
      },
    },
  ];
}

/** 详情页字段 */
export function useDetailSchema(): DescriptionItemSchema[] {
  return [
    {
      label: '提示词编号',
      field: 'code',
    },
    {
      label: '提示词图标',
      field: 'icon',
      render: (val, data) => {
        return val
          ? h(IconifyIcon, {
              icon: val,
              color: data?.color,
            })
          : '无';
      },
    },
    {
      label: '颜色',
      field: 'color',
      render: (val) => {
        return val
          ? h(ColorPicker, {
              modelValue: val,
              disabled: true,
            })
          : '无';
      },
    },
    {
      label: '提示词名称',
      field: 'name',
    },
    {
      label: '提示词描述',
      field: 'description',
      render: (val) =>
        h('div', {
          innerHTML: val,
          class: 'whitespace-pre-wrap text-sm leading-relaxed',
        }),
    },
    {
      label: '提示词内容',
      field: 'content',
      render: (val) => h('div', { innerHTML: val }),
    },
    {
      label: '提示词使用说明',
      field: 'usageDescription',
      render: (val) =>
        h('div', {
          innerHTML: val,
          class: 'whitespace-pre-wrap text-sm leading-relaxed',
        }),
    },
    {
      label: '分类ID',
      field: 'categoryId',
    },
    {
      label: '版本号',
      field: 'versionNum',
    },
    {
      label: '语言',
      field: 'languageName',
    },
    {
      label: '适用的AI模型',
      field: 'modelTypeName',
    },
    {
      label: '可见性',
      field: 'visibility',
    },
    {
      label: '状态',
      field: 'statusName',
    },
    {
      label: '系统内置',
      field: 'systemFlag',
      render: (val) =>
        h(DictBadge, {
          value: val,
        }),
    },
    {
      label: '使用次数',
      field: 'usageCount',
    },
    {
      label: '收藏次数',
      field: 'collect',
    },
    {
      label: '评分',
      field: 'rating',
    },
    {
      label: '其他参数',
      field: 'metadata',
      render: (val) => h('div', { innerHTML: val }),
    },
    {
      label: '效果展示图片',
      field: 'effectImg',
      render: (val) => {
        return val
          ? h(Image, {
              src: getFileUrl(val, true),
              width: 60,
              height: 60,
            })
          : '未上传';
      },
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'code',
      label: '提示词编号',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入提示词编号',
      },
    },
    {
      fieldName: 'name',
      label: '提示词名称',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入提示词名称',
      },
    },
    {
      fieldName: 'categoryId',
      label: '分类ID',
      component: 'ApiTreeSelect',
      componentProps: {
        allowClear: true,
        api: async () => {
          const data = await getPromptCategoriesList({});
          return handleTree(data);
        },
        labelField: 'name',
        valueField: 'id',
        childrenField: 'children',
        placeholder: '请选择上级提示词分类',
        treeDefaultExpandAll: true,
      },
    },
    {
      fieldName: 'language',
      label: '语言',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.AI_LANGUAGE),
        placeholder: '请选择语言',
      },
    },
    {
      fieldName: 'modelType',
      label: '适用的AI模型',
      component: 'ApiSelect',
      componentProps: {
        allowClear: true,
        api: getSimpleList,
        labelField: 'title',
        valueField: 'id',
        placeholder: '请选择适用的AI模型',
      },
    },
    {
      fieldName: 'visibility',
      label: '可见性',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.AI_PROMPT_VISIBILITY, 'number'),
        placeholder: '请选择可见性',
      },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.AI_PROMPT_STATUS, 'number'),
        placeholder: '请选择状态',
      },
    },
    {
      fieldName: 'systemFlag',
      label: '系统内置',
      component: 'Select',
      componentProps: {
        allowClear: true,
        placeholder: '请选择是否系统内置',
        options: [
          {
            label: '是',
            value: true,
          },
          {
            label: '否',
            value: false,
          },
        ],
      },
    },
    {
      fieldName: 'createTime',
      label: '创建时间',
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        allowClear: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<AiPromptTemplatesApi.PromptTemplates>['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: '提示词id',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'code',
      title: '提示词编号',
      minWidth: 120,
    },
    {
      field: 'name',
      title: '提示词名称',
      minWidth: 200,
    },
    {
      field: 'categoryName',
      title: '分类名称',
      minWidth: 120,
    },
    {
      field: 'description',
      title: '提示词描述',
      minWidth: 300,
    },

    {
      field: 'versionNum',
      title: '版本号',
      minWidth: 80,
    },
    {
      field: 'language',
      title: '语言',
      minWidth: 60,
    },
    {
      field: 'icon',
      title: '图标',
      minWidth: 60,
      slots: { default: 'icon' },
    },
    {
      field: 'color',
      title: '颜色',
      minWidth: 80,
      slots: { default: 'color' },
    },
    {
      field: 'modelTypeName',
      title: '适用的AI模型',
      minWidth: 200,
    },
    {
      field: 'visibility',
      title: '可见性',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.AI_PROMPT_VISIBILITY },
      },
    },
    {
      field: 'status',
      title: '状态',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.AI_PROMPT_STATUS },
      },
    },
    {
      field: 'sort',
      title: '排序',
      minWidth: 80,
    },
    {
      field: 'systemFlag',
      title: '系统内置',
      minWidth: 120,
      cellRender: {
        name: 'CellDictBadge',
      },
    },
    {
      field: 'usageCount',
      title: '使用次数',
      minWidth: 100,
    },
    {
      field: 'collect',
      title: '收藏次数',
      minWidth: 80,
    },
    {
      field: 'rating',
      title: '评分',
      minWidth: 80,
    },
    {
      field: 'effectImg',
      title: '效果图片',
      minWidth: 80,
      slots: { default: 'effectImg' },
    },
    {
      field: 'createTime',
      title: '创建时间',
      minWidth: 160,
      formatter: 'formatDateTime',
    },
    {
      field: 'actions',
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
